<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>文章分类管理</title>
</head>
<body>
<cms-layout layout="admin/layout/master">
    <div id="app">
        <el-button type="primary" @click="addRow">添加</el-button>
        <br><br>
        <el-table
                :data="tableData"
                style="width: 100%">
            <el-table-column
                    prop="id"
                    label="ID">
            </el-table-column>
            <el-table-column
                    prop="nameTranslate.content"
                    label="名称">
            </el-table-column>
            <el-table-column
                    prop="updateTime"
                    label="修改时间">
            </el-table-column>
            <el-table-column
                    label="操作">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="warning"
                            @click="editRow(scope.row)"
                    >
                        修改
                    </el-button>
                    <el-button
                            size="mini"
                            type="danger"
                            @click="deleteRow(scope.row)"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                @current-change="handlePageChange"
                :current-page="params.page"
                :page-size="params.pageSize"
                layout="total, prev, pager, next"
                :total="params.total">
        </el-pagination>
        <el-dialog title="新增/修改" :visible.sync="dialog.dialogFormVisible">
            <el-form :model="category">
                <el-form-item label="名称">
                    <el-input v-model="category.name" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialog.dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveRow">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</cms-layout>
</body>
<script th:inline="javascript">
    let app = new Vue({
        el: '#app',
        data: function () {
            return {
                params: {
                    page: 1,
                    pageSize: 15,
                    total: 0,
                },
                tableData: [],
                dialog: {
                    dialogFormVisible: false,
                },
                category: {
                    name: '',
                },
            }

        },
        created() {
            this.initData()
        },
        methods: {
            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            },
            handlePageChange(val) {
                this.params.page = val;
                this.params.offset = (val - 1) * this.params.pageSize;
                this.initData()
            },
            initData() {
                let that = this;
                axios.get('/admin/article_category/list', {params: this.params}).then(res => {
                    res = res.data;
                    if (res.code == 0) {
                        that.tableData = res.data.content;
                        that.params.total = res.data.totalElements;
                    } else {
                        that.tableData = [];
                    }
                    console.log(res);

                });
            },
            addRow() {
                this.category.id = 0;
                this.category.name = "";
                this.dialog.dialogFormVisible = true;
            },
            editRow(row) {
                this.category.id = row.id;
                this.category.name = row.nameTranslate.content;
                this.dialog.dialogFormVisible = true;
            },
            deleteRow(row) {
                let that = this;

                this.$confirm('确定要删除吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {

                    axios.delete('/admin/article_category/destroy/' + row.id, {}).then(function (res) {
                        res = res.data;
                        if (res.code == 0) {
                            that.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            that.initData();
                            that.dialog.dialogFormVisible = false;
                        } else {
                            that.$message({
                                type: 'error',
                                message: err.msg
                            });
                        }
                    }).catch(function (err) {
                        that.$message({
                            type: 'error',
                            message: err.msg
                        });
                    }).finally(function () {

                    });

                }).catch(() => {

                });


            },
            saveRow() {
                let that = this;
                //TODO 必填验证
                if (this.category.id > 0) {
                    axios.put('/admin/article_category/update/' + this.category.id, this.category).then(function (res) {
                        res = res.data;
                        that.$message({
                            type: 'success',
                            message: res.msg
                        });
                        if (res.code == 0) {
                            that.initData();
                        }
                        that.dialog.dialogFormVisible = false;
                    }).catch(function (err) {
                        that.$message({
                            type: 'error',
                            message: err.msg
                        });
                    }).finally(function () {

                    });
                } else {
                    axios.post('/admin/article_category/store', this.category).then(function (res) {
                        res = res.data;
                        that.$message({
                            type: 'success',
                            message: res.msg
                        });
                        if (res.code == 0) {
                            that.initData();
                        }
                        that.dialog.dialogFormVisible = false;
                    }).catch(function (err) {
                        that.$message({
                            type: 'error',
                            message: err.msg
                        });
                    }).finally(function () {

                    });
                }
            }

        },
    })

</script>
</html>